﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace katOS
{
    class Shell
    {
        public void Shell_Construct() 
        {
            string an_instruction;
            Console.WriteLine(Prompt());
            do
            {
                an_instruction = Console.ReadLine();
                Parse(an_instruction);
            }
            while (!an_instruction.Equals("exit"));
        }

        public String Prompt() 
        {
            string welcome = "katOS [Version 1.0]\n" +
                           "GNU General Public License v3 <http://code.google.com/p/karenandys-rep/> \n" +
                           "show root!!\n";
            return welcome;
        }

        public void Parse (String instruction)
        {
            instruction = instruction.TrimStart(' ');
            instruction = instruction.TrimEnd(' ');
            instruction = instruction.TrimStart('\t');
            instruction = instruction.TrimEnd('\t');
            while (instruction.Contains("   ")) 
            {
                instruction = instruction.Replace(" "," ");
            }

            string[] complete_instruction = instruction.Split(' ');
            IsSystemProgram(complete_instruction);
        }

        public void IsSystemProgram(string[] instruction)
        {
            SystemPrograms a_program = new SystemPrograms();
            Scheduler a_scheduler = new Scheduler();
            switch (instruction[0])
            { 
                case "ls":
                    a_program.ls();
                    break;
                case "kill":
                    Console.WriteLine("check the killing arguments!");
                    break;
                case "ps":
                    a_program.ps();
                    break;
                case "exit":
                    Console.WriteLine("Good bye");
                    //here terminate the threads
                    break;
                case "help":
                    Console.WriteLine("Show help"); //TODO
                    break;
                default:
                    string file = instruction[0];
                    a_scheduler.AddProcess(file);
                    break;
            }
        }
    }
}
